package com.songlanyun.common.config;

import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.redisson.config.*;
@Configuration
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;

    @Bean
    public RedissonClient redissonClient()
    {
        Config config =new Config();
        // use "rediss://" for SSL connection
        //集群
        //config.useClusterServers().addNodeAddress("perredis://127.0.0.1:7181");
        //单机redis
        if(!StringUtils.isBlank(password))
        {
            config.useSingleServer().setAddress("redis://"+host+":"+port).setPassword(password).setDatabase(database);
        }
        else
        {
            config.useSingleServer().setAddress("redis://"+host+":"+port).setDatabase(database);
        }


        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}
